package com.kstech.gw;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.kstech.gw.entity.JsonUserEntity;
import com.kstech.gw.utils.JsonUtils;
import com.kstech.gw.utils.MyHttpUtils;
import com.kstech.gw.utils.ToastUtils;

import java.util.HashMap;

public class LoginActivity extends Activity implements MyHttpUtils.MyHttpCallback{
    private EditText etAccount;
    private EditText etPass;
    private String account;
    private String password;
    private AlertDialog.Builder builder;
    private AlertDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    public void initView(){
        etAccount = (EditText) findViewById(R.id.et_account);
        etPass = (EditText) findViewById(R.id.et_password);
        builder = new AlertDialog.Builder(this);
    }


    public void login(View view) {
        account = etAccount.getText().toString();
        password = etPass.getText().toString();
        account = account.trim();
        password = password.trim();
        if(account.equals("") || password.equals("")){
            Toast.makeText(this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        dialog = builder.setTitle("登陆信息").setMessage("正在登录中---").create();
        dialog.show();
        HashMap<String, String> map = new HashMap<>();
//        map.put("tenantId","7");"liushengzhang@test.com"
//        map.put("userId","175");"123456"
        map.put("account", account);
        map.put("password", password);
        String url = "http://111.206.163.56:8990/gw-factory/phone/user/login";
        new MyHttpUtils().xutilsPost("LOGIN",url,map,this);
    }
    @Override
    public void onSuccess(String result, String whereRequest) {
        JsonUserEntity jsonUserEntity = (JsonUserEntity) JsonUtils.fromJson(result,JsonUserEntity.class);
        Log.e("hahah",jsonUserEntity.isSuccess()+"  \n"+jsonUserEntity.getResult()+"  \n"+jsonUserEntity.getResult().getStatus());
        if(jsonUserEntity.isSuccess()){
            dialog.cancel();
            startActivity(new Intent(this,HomeActivity.class));
            ToastUtils.showToast(this,"欢迎您");
            finish();
        }

    }

    @Override
    public void onError(String errorMsg, String whereRequest) {
        Log.e("hahah",errorMsg);
        dialog.setTitle("登录失败");
        dialog.setMessage("网络异常");
        dialog.cancel();
        startActivity(new Intent(this,HomeActivity.class));
    }
}
